<?php namespace Summer\Shop\Controllers;

use Backend\Classes\Controller;
use BackendMenu;
use Flash;
use Summer\Shop\Models\CommentModel;

class Comment extends Controller
{
    public $implement = [        'Backend\Behaviors\ListController',        'Backend\Behaviors\FormController'    ];
    
    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';

    public function __construct()
    {
        parent::__construct();
        BackendMenu::setContext('Summer.Shop', 'main-menu-item-shop', 'side-menu-item-comment');
    }

    public function index()
    {
        $this->addJs('/plugins/rainlab/user/assets/js/bulk-actions.js');

        $this->asExtension('ListController')->index();
    }
    /**
     * Perform bulk action on selected users
     */
    public function index_onBulkAction()
    {
        if (
            ($bulkAction = post('action')) &&
            ($checkedIds = post('checked')) &&
            is_array($checkedIds) &&
            count($checkedIds)
        ) {

            foreach ($checkedIds as $commentId) {

                if (!$comment = CommentModel::find($commentId)) {
                    continue;
                }

                switch ($bulkAction) {
                    case 'show':
                        $comment->is_show=1;
                        $comment->save();
                        break;

                    case 'unshow':
                        $comment->is_show=0;
                        $comment->save();
                        break;
                }
            }
            Flash::success('操作成功');
        }
        else {
            Flash::error('操作失败');
        }

        return $this->listRefresh();
    }
}
